package com.gtw.modules.sys.controller;

import com.gtw.common.utils.PageUtils;
import com.gtw.common.utils.Query;
import com.gtw.common.utils.R;
import com.gtw.modules.entity.TbIntegral;
import com.gtw.modules.sys.service.TbIntegralService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/** 积分数据controller
 * Created by wuye on 2018/2/28.
 */
@RestController
@RequestMapping("/sys/integral2")
public class SysTbIntegralController {

    @Autowired
    private TbIntegralService integralService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:integral2:list")
    public R list(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);
        List<TbIntegral> integralList = integralService.queryList(query);
        int total = integralService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(integralList, total, query.getLimit(), query.getPage());
        return R.ok().put("page", pageUtil);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:integral2:info")
    public R info(@PathVariable("id") Integer id){
        TbIntegral integral = integralService.queryObject(id);
        return R.ok().put("integral", integral);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:integral2:save")
    public R save(@RequestBody TbIntegral integral){
        integral.setIsdelete(0);
        integralService.save(integral);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:integral2:update")
    public R update(@RequestBody TbIntegral integral){
        integralService.update(integral);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:integral2:delete")
    public R delete(@RequestBody Integer[] ids){
        for (Integer id: ids) {
            TbIntegral tbIntegral = integralService.queryObject(id);
            tbIntegral.setIsdelete(1);
            integralService.update(tbIntegral);
        }
        return R.ok();
    }
}
